package com.nbsaas.boot.data.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.IdentityHashMap;
import java.util.Map;

import static freemarker.template.Configuration.VERSION_2_3_32;


public class FreemarkerUtils {


    public static String process(String templateNote, Map<String, Object> model) {
        if (templateNote == null) {
            return null;
        }
        Configuration configuration = new Configuration(VERSION_2_3_32);
        //configuration.setNumberFormat("0");  // 禁用数字格式化，显示纯数字
        configuration.setNumberFormat("0.##");
        StringWriter out = new StringWriter();
        try {
            new Template("template", new StringReader(templateNote), configuration).process(model, out);
        } catch (Exception e) {
            Object modelKey = model.get("model");
            System.out.println("modelKey:" + modelKey);
            System.out.println("templateNote:" + templateNote);
            e.printStackTrace();
        }
        return out.toString();
    }

    public static void main(String[] args) {
        System.out.println(process(" ${1234567?c}///${1234.567a}", new IdentityHashMap<>()));
    }
}
